/**
 *	@author: Kathleen Go
 *	date created: January 5, 2006
 *	version: 1.02
 * modified: April 13, 2006
 */

package text.ia;

import java.util.Vector;
import java.sql.ResultSet;
import java.sql.SQLException;

import text.util.DBConnector;

public class TExtMorphologyProcessor{
	
	Vector vResults;
	char cTarget;
	
	public TExtMorphologyProcessor(char cTarget){
		vResults = new Vector();
		this.cTarget = cTarget;
	}
	
	/**	Constructs the query that will perform Morphological Analysis on the given
	 *		word (strWord) and returns its root word.
	 * 
	 *	Returns the {@link Vector} of root words of strWord and the corresponding forms if vForWFW is true.
	 *
	 *	@param strWord a {@link String} whose root word is to be derived
	 *	@param cLookfor a char data indicating the language of strWord
	 *	@param bForWFW a boolean data indicating whether the result is to be used in
	 *		word-for-word translation or not (unit alignment)
	 *	@return the {@link Vector} containing the root words of strWord and the corresponding forms if vForWFW is true.
	 *		index 0 - Vector of root words of strWord
	 *		index 1 - Vector of forms associated with each root word in index 0
	 */
	public Vector wordMA(String strWord, char cLookFor, boolean bForWFW){
		String strTargetField;
		String strQuery;
		
		if(cTarget == cLookFor){
			if(cTarget == 'e'){
				strTargetField = "filipinoRootWord";
				strQuery = "Select * from FilipinoMA where filipinoWord = '" + strWord + "'";
			}
			else{
				strTargetField = "englishrootWord";
				strQuery = "Select * from EnglishMA where englishWord = '" + strWord + "'";
			}
		}
		else{
			if(cLookFor == 'e'){
				strTargetField = "englishWord";
				strQuery = "Select * from EnglishMA where englishWord like '" + strWord + "%'";
			}
			else{
				strTargetField = "filipinoWord";
				strQuery = "Select * from FilipinoMA where filipinoWord like '" + strWord + "%'";
			}
		}
		return dbLookUp(strQuery, strTargetField, bForWFW);
	}
	
	/**	Constructs the query that will perform Morphological Generation on the given
	 *		root word (strWord) and returns its derivatives.
	 * 
	 *	Returns the {@link Vector} of derivatives of strWord.
	 *
	 *	@param strWord a {@link String} whose derivatives are to be generated
	 *	@param strForm a {@link String} indicating the form of strWord's derivative
	 *	@return the {@link Vector} containing the derivatives of strWord
	 */
	public Vector wordMG(String strWord, String strForm){
		Vector vResult = null;
		String strTargetField;
		String strQuery;
		
		if(cTarget == 'f'){
			strTargetField = "filipinoWord";
			if(strForm != null)
				strQuery = "Select " + strTargetField + " from FilipinoMA where filipinoRootWord = '" + strWord + "' AND Form = '" + strForm + "'";
			else
				strQuery = "Select " + strTargetField + " from FilipinoMA where filipinoRootWord = '" + strWord + "'";
		}
		else{
			strTargetField = "englishWord";
			if(strForm != null)
				strQuery = "Select " + strTargetField + " from EnglishMA where englishRootWord = '" + strWord + "' AND Form = '" + strForm + "'";
			else
				strQuery = "Select " + strTargetField + " from EnglishMA where englishRootWord = '" + strWord + "'";
		}
		vResult = dbLookUp(strQuery, strTargetField, false);
		if(vResult != null)
			return 	vResult;
		
		return null;
		
	}
	
	/**	Performs the actual DB lookup.
	 *	Returns the Vector of results.
	 */
	public Vector dbLookUp(String strQuery, String strTargetField, boolean isMAForWFW){
		ResultSet rsEquivWords;
		Vector vForm = null;
		
		if(isMAForWFW)
			vForm = new Vector();
		
		rsEquivWords = DBConnector.select(strQuery);
		
		vResults.clear();
		
		try{
			if(isMAForWFW){
				while(rsEquivWords.next()){
					String strResult = rsEquivWords.getString(strTargetField);
					String strForm = rsEquivWords.getString("Form");
					if(strResult != null && strForm != null){
						vResults.add(new String(strResult));
						vForm.add(new String(strForm));
					}
				}
			}
			else{
				while(rsEquivWords.next()){
					String strResult = rsEquivWords.getString(strTargetField);
					if(strResult != null)
						vResults.add(new String(strResult));
				}
			}
		}	
		catch(SQLException e){}

		if(vResults.size() == 0)
			return null;
		if(isMAForWFW)
			vResults.add(vForm);
		return vResults;
	}
}